<template>
  <view class="container">
    <!-- 用户信息 -->
    <view v-if="isAuthInfo" class="userinfo">
      <view class="userinfo-con">
        <view class="userinfo-avatar">
          <image v-if="userInfo.avatar" style="width: 100%;height: 100%;" :src="userInfo.avatar" />
          <image v-else src="@/static/images/icon/head04.png" />
        </view>
        <view class="userinfo-name">
          <view>{{ userInfo.realname ? userInfo.realname : "用户昵称" }}</view>
        </view>
      </view>
    </view>
    <view v-else class="userinfo-none">
      <view class="default-pic" @tap="toLogin">
        <image src="@/static/images/icon/head04.png" />
      </view>
      <view class="none-login" @tap="toLogin">
        <button class="unlogin">
          未登录
        </button>
        <button class="click-login">
          点击登录账号
        </button>
      </view>
    </view>
    <!-- end 用户信息 -->

    <view class="list-cont">
      <!-- 列表项 -->
      <view class="my-menu">
        <view class="memu-item" @tap="goPage(pages[0])">
          <view class="i-name">
            <image src="@/static/images/icon/getCoupon.png" />
            <text>信息填报</text>
          </view>
          <view class="arrowhead" />
        </view>
        <view class="memu-item" @tap="goPage(pages[1])">
          <view class="i-name">
            <image src="@/static/images/icon/getCoupon.png" />
            <text>病史录入</text>
          </view>
          <view class="arrowhead" />
        </view>
        <view class="memu-item" @tap="goPage(pages[2])">
          <view class="i-name">
            <image src="@/static/images/icon/promotion.png" />
            <text>预约记录</text>
          </view>
          <view class="arrowhead" />
        </view>
        <view class="memu-item" @tap="goPage(pages[3])">
          <view class="i-name">
            <image src="@/static/images/icon/getCoupon.png" />
            <text>报告查询</text>
          </view>
          <view class="arrowhead" />
        </view>
        <view class="memu-item" @tap="goPage()">
          <view class="i-name">
            <image src="@/static/images/icon/myAddr.png" />
            <text>设置</text>
          </view>
          <view class="arrowhead" />
        </view>
      </view>
      <!--end 列表项 -->

      <!-- <view v-if="isAuthInfo" class="log-out" @tap="logout">
        <view class="log-out-n">
          <text>退出登录</text>
        </view>
      </view> -->
    </view>
  </view>
</template>

<script setup>
  const data = reactive({
    pages: [
      '/pages/patientArchive/patientArchive',
      '/pages/medicalHistory/medicalHistory',
      '/pages/reserveRecord/reserveRecord',
      '/',
    ],
    isAuthInfo: false,
    userInfo: {},
  });
  const {
    pages,
    isAuthInfo,
    userInfo
  } = toRefs(data);

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad(() => {
    console.log('user-on-load');
  })
  onShow(() => {
    console.log('user-on-show');
    let loginUserInfo = uni.getStorageSync('loginUserInfo');
    data.isAuthInfo = !util.judgeNull(loginUserInfo);
    data.userInfo = loginUserInfo || {};
  })
  onMounted(async () => {
    console.log('user-on-mounted');
  })

  const goPage = (page) => {
    if (onLogin()) {
      let item = {
        patientPickerShow: true,
        okBackUrl: '/pages/user/user'
      }
      uni.navigateTo({
        url: page + '?item=' + encodeURIComponent(JSON.stringify(item)),
      });
    }
  }

  const onLogin = () => {
    if (util.judgeNull(uni.getStorageSync('Token'))) {
      http.toReLaunchLogin('/pages/user/user');
      return;
    } else {
      return onSignContract();
    }
  }

  const onSignContract = () => {
    let loginUserInfo = uni.getStorageSync('loginUserInfo') || {};
    if ((util.judgeNull(loginUserInfo.agreeAgreementPolicy) || loginUserInfo.agreeAgreementPolicy == '0')) {
      uni.reLaunch({
        url: '/pages/index/index', // 需要重新打开的页面路径
      });
      return false;
    }
    return true;
  }

  /**
   * 去登陆
   */
  const toLogin = () => {
    http.toReLaunchLogin('/pages/user/user', 请进行登录);
  }

  /**
   * 退出登录
   */
  const logout = () => {
    tip.loading();
    http.post('/logOut').then(() => {
      util.removeTabBadge();
      uni.removeStorageSync('loginUserInfo');
      uni.removeStorageSync('token');
      tip.toast('退出成功');
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/index/index',
          complete() {
            tip.hide();
          }
        });
      }, 1000)
    }).catch(err => {
      console.log('登出失败 ==> ', err);
      tip.hide();
    })
  }
</script>

<style scoped lang="scss">
  @use './user.scss';
</style>
